package com.my.selectcoursesystem.controller;

import com.my.selectcoursesystem.bean.Course;
import com.my.selectcoursesystem.bean.SC;
import com.my.selectcoursesystem.bean.SelectStudent;
import com.my.selectcoursesystem.bean.Teacher;
import com.my.selectcoursesystem.service.serviceImpl.CourseServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.SelectCourseServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.SelectStudentServiceImpl;
import com.my.selectcoursesystem.service.serviceImpl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TeacherController {

    @Autowired
    private TeacherServiceImpl teacherService;
    @Autowired
    private CourseServiceImpl courseService;
    @Autowired
    private SelectStudentServiceImpl selectStudentService;

    public String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();//根据请求数据，找到cookie数组
        String value = "";
        if (null == cookies) {//如果没有cookie数组
            System.out.println("没有cookie");
            value = "";
        } else {
            for (Cookie cookie : cookies) {
                System.out.println("cookieName:" + cookie.getName() + ",cookieValue:" + cookie.getValue());
                if (cookie.getName().equals(name)) {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        return value;
    }

    public TeacherController(TeacherServiceImpl teacherService, CourseServiceImpl courseService, SelectStudentServiceImpl selectStudentService) {
        this.teacherService = teacherService;
        this.courseService = courseService;
        this.selectStudentService = selectStudentService;
    }

    public TeacherController() {
    }

    @RequestMapping("/addCourse")
    public String getTeacherInfo(Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        List<Course> courses = this.courseService.getAllCourse();
        model.addAttribute("cid", "0000" + (courses.size() + 1));
        model.addAttribute("courses", courses);
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        return "teacher/doAddCou";
    }
    @RequestMapping(value = "/addedCourse")
    public String addedCourse(String courseclass, String coursetime, String courseweek, String cid,String tid, String classroom, String credits,String type, String totalnum, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        Course course = new Course();
        Teacher teacher = this.teacherService.getTeacherById(sid);
        course.setCid(cid);
        course.setCoursetime(coursetime);
        course.setCourseweek(courseweek);
        course.setCollege(teacher.getCollege());
        course.setTeacherid(tid);
        course.setCname(courseclass);
        course.setClassroom(classroom);
        course.setType(type);
        course.setCredits(Integer.valueOf(credits));
        course.setTotalnum(Integer.valueOf(totalnum));
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        model.addAttribute("u", "teacher");
        Course insert = this.courseService.insert(course);
        if (insert != null) {
            model.addAttribute("msg", "添加成功!");
            return "success";
        }else {
            model.addAttribute("msg", "添加失败!");
            return "fail";
        }
    }

    @RequestMapping(value = "/manageCourse/{sid}")
    public String manageCourse(@PathVariable(value = "sid") String sid, Model model, HttpServletRequest request) {
        String s_id = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", s_id);
        model.addAttribute("sname", sname);
        List<Course> courses = this.courseService.getCourseByTid(sid);
        model.addAttribute("courses", courses);
        return "teacher/manageCourse";
    }

    @RequestMapping(value = "/modifyCourse/{cid}")
    public String modifyCourse(@PathVariable(value = "cid") String cid, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        Course course = this.courseService.getCourseByCidAndTid(cid, sid);
        model.addAttribute("sid", sid);
        model.addAttribute("cid", cid);
        model.addAttribute("sname", sname);
        model.addAttribute("course", course);
        return "teacher/modCouPlan";
    }

    // 删除课程
    @RequestMapping(value = "/deleteCourse/{cid}", method = RequestMethod.DELETE)
    public String deleteCourse(@PathVariable(value = "cid") String cid, Model model, HttpServletRequest request) {
        String sid = this.getCookie(request, "sid");
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Boolean isDel = this.courseService.deleteByCidAndTid(cid, sid);
        if (isDel) {
            model.addAttribute("msg", "删除成功!");
            model.addAttribute("u", "teacher");
            return "success";
        } else {
            model.addAttribute("msg", "删除失败!");
            model.addAttribute("u", "teacher");
            return "fail";
        }
    }
    @RequestMapping(value = "/show/{sid}")
    public String showStudents(@PathVariable(value = "sid")String sid, Model model, HttpServletRequest request){
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        List<SelectStudent> students = this.selectStudentService.getAllByTid(sid);
        model.addAttribute("students", students);
        return "teacher/printStudent";
    }
    @RequestMapping(value = "/myt/{sid}")
    public String goToMy(@PathVariable(value = "sid")String sid, Model model, HttpServletRequest request){
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Teacher teacher = this.teacherService.getTeacherById(sid);
        model.addAttribute("teacher", teacher);
        return "teacher/queryVita";
    }
    @RequestMapping(value = "/modifyt/{sid}")
    public String modifyPass(@PathVariable(value = "sid")String sid, Model model, HttpServletRequest request){
        String sname = this.getCookie(request, "sname");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Teacher teacher = this.teacherService.getTeacherById(sid);
        model.addAttribute("teacher", teacher);
        return "teacher/modityPw";
    }
    @RequestMapping(value = "/modifiedPass")
    public String modifiedPass(String tpassword, Model model, HttpServletRequest request){
        String sname = this.getCookie(request, "sname");
        String sid = this.getCookie(request, "sid");
        model.addAttribute("sid", sid);
        model.addAttribute("sname", sname);
        Teacher teacher = this.teacherService.getTeacherById(sid);
        teacher.setTpassword(tpassword);
        Boolean isMod = this.teacherService.modifyTeacherPass(teacher);
        if (isMod) {
            model.addAttribute("msg", "修改成功!");
            model.addAttribute("u", "teacher");
            return "success";
        } else {
            model.addAttribute("msg", "修改失败!");
            model.addAttribute("u", "teacher");
            return "fail";
        }
    }
}
